"=c@@b _  
" ( / ) )  vimrc by mhugh
"  / / /   2019-01-16
" / / (_   ail
"/         vim 8

""""""""""""""""""""""""" c@@- [ Global Variables ]"""""""""""""""""""""""""
set runtimepath^=$HOME/.vim runtimepath+=$HOME/.vim/after
let &packpath = &runtimepath
let mapleader=','

" ultisnips
let g:UltiSnipsExpandTrigger             = '<c-j>'
let g:UltiSnipsJumpForwardTrigger        = '<c-j>'
let g:UltiSnipsJumpBackwardTrigger       = '<c-k>'
let g:UltiSnipsEditSplit                 = 'vertical'
let g:UltiSnipsSnippetDirectories        = ['ultisnips']

" syntastic
let g:syntastic_always_populate_loc_list = 0
let g:syntastic_auto_loc_list            = 1
let g:syntastic_check_on_open            = 1
let g:syntastic_check_on_wq              = 0
let g:syntastic_javascript_checkers      = ['eslint']
let g:syntastic_python_checkers          = ['pylint']
let g:syntastic_css_checkers             = ['csslint']
let g:syntastic_html_checkers            = ['HTMLHint','jshint']

" emmet
let g:user_emmet_mode                    = 'inv'

" editor config
let g:EditorConfig_exclude_patterns      = ['fugitive://.*', 'scp://.*']
if has('win32')
    let g:EditorConfig_exec_path         = "C:/ProgramData/chocolatey/bin/editerconfig.exe"
elseif has('osx')
    let g:EditorConfig_exec_path         = "~/.local/bin/editorconfig"
else
    let g:EditorConfig_exec_path         = "/usr/bin/editorconfig"
endif

" polyglot
let g:polyglot_disabled                  = ['css']

" indentLine
let g:indentLine_setColors               = 0

" vim-markdown
let g:vim_markdown_conceal               = 0
let g:vim_markdown_folding_level         = 2
let g:vim_markdown_toc_autofit           = 1
let g:vim_markdown_fenced_languages      = ['c++=cpp', 'viml=vim', 'bash=sh', 'ini=dosini', 'jsx=js']
let g:vim_markdown_math                  = 1
let g:vim_markdown_frontmatter           = 1
let g:vim_markdown_toml_frontmatter      = 1
let g:vim_markdown_json_frontmatter      = 1
let g:vim_markdown_new_list_item_indent  = 2
let g:vim_markdown_auto_insert_bullets   = 0
let g:vim_markdown_new_list_item_indent  = 0

" vim-javascript
let g:javascript_plugin_jsdoc            = 1
let g:jsx_ext_required                   = 1

" vim-easyclip
let g:EasyClipUseCutDefaults             = 0
let g:EasyClipUsePasteToggleDefaults     = 0

"""""""""""""""""""""""" c@@- [ Regist Vim Plugins ]""""""""""""""""""""""""
call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-unimpaired'         "  Pairs of handy bracket mappings
Plug 'scrooloose/nerdtree'
Plug 'honza/vim-snippets'
Plug 'SirVer/ultisnips'
Plug 'kopischke/vim-stay'           " saving views
Plug 'mileszs/ack.vim'              " perl search in directories
Plug 'kien/ctrlp.vim'               " Full path fuzzy file, buffer, mru, tag, ... finder
Plug 'Valloric/YouCompleteMe'       " identifier complete engine
Plug 'terryma/vim-multiple-cursors'
Plug 'svermeulen/vim-easyclip'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'

Plug 'editorconfig/editorconfig-vim'
Plug 'kshenoy/vim-signature'        " show marks in gutter
Plug 'sheerun/vim-polyglot'         " language syntaxes
Plug 'jiangmiao/auto-pairs'         " auto pairing
Plug 'Konfekt/FastFold'
Plug 'tpope/vim-surround'
Plug 'ap/vim-css-color'
Plug 'chrisbra/unicode.vim'         " input unicode and digraph
Plug 'majutsushi/tagbar'            " tags & definitions
Plug 'scrooloose/nerdcommenter'
Plug 'tpope/vim-projectionist'
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-speeddating'
Plug 'junegunn/vim-easy-align'
Plug 'Yggdroot/indentLine'           " display indent lines
Plug 'scrooloose/syntastic'          " lint
Plug 'Chiel92/vim-autoformat'
Plug 'airblade/vim-gitgutter'
Plug 'tpope/vim-fugitive'
Plug 'easymotion/vim-easymotion'

" colorschemes
" Plug 'altercation/vim-colors-solarized'
Plug 'chriskempson/base16-vim'

" html
Plug 'mattn/emmet-vim'
" Plug 'digitaltoad/vim-pug'
" Plug 'tpope/vim-haml'

" markdown
Plug 'godlygeek/tabular'
Plug 'plasticboy/vim-markdown'

" javascript
Plug 'pangloss/vim-javascript'
Plug 'mxw/vim-jsx'
Plug 'leafgarland/typescript-vim'
Plug 'kchmck/vim-coffee-script'
Plug 'posva/vim-vue'
call plug#end()

""""""""""""""""""""""""""" c@@- [ Key Bindings ]"""""""""""""""""""""""""""
" vim-easyclip
if has('gui_running')
    if has('osx') || has('win32')
        set clipboard=unnamed
    else
        " set as "plus" on linux to use system clipboard
        set clipboard=unnamedplus 
    endif
else
    " set to empty for no gui
    set clipboard=
endif

nmap d   <Plug>MoveMotionPlug
xmap d   <Plug>MoveMotionXPlug
nmap dd  <Plug>MoveMotionLinePlug
nmap ]y  <Plug>EasyClipSwapPasteForward
nmap [y  <Plug>EasyClipSwapPasteBackwards

"""""""""""""""""""""""""""" c@@- [ Customized ]""""""""""""""""""""""""""""
" shortcuts
nnoremap <leader>h   :helpgrep<space>
nnoremap <leader>s   :w<cr>
nnoremap <leader>k   :bd<cr>
nnoremap <leader>l   :ls<cr>
nnoremap <leader>b   :bm<cr>
nnoremap <leader>p   :IPaste<cr>
nnoremap <leader>gp  :registers<cr>
nnoremap <leader>d   :NERDTree<space>
nnoremap <leader>q   :q<cr>
nnoremap <leader>gq  :q!<cr>
nnoremap <leader>xq  :qa<cr>
nnoremap <leader>gxq :qa!<cr>
nnoremap <leader>t   :TagbarToggle<cr>
xnoremap <leader>a   :EasyAlign<space>
nnoremap <leader>f   :Autoformat<cr>
    nmap <leader>j   <Plug>(easymotion-bd-w)
    vmap <leader>j   <Plug>(easymotion-bd-w)
nnoremap <leader>/   :Ack!<Space>
nnoremap <leader>v   :vert<space>
nnoremap <leader>xs  :mks! .vimproject<cr>

" gui open directory
if has('osx')
    nnoremap <leader>e :exe ':silent !open %:p:h'<cr>
elseif has('win32')
else
    nnoremap <leader>e :Ex<cr>
endif

" gui open file
if has('osx')
    nnoremap <leader>xw :exe ':silent !open %'<cr>
elseif has('win32')
else
    nnoremap <leader>xw :Ex<cr>
endif

" quickly edit your macros
nnoremap <silent> <leader>m   :<c-u><c-r><c-r>='let @'. v:register .' = '. string(getreg(v:register))<cr><c-f><left>
" Switch to UTF-8 encoding
nnoremap <silent> <leader>xu  :e ++enc=utf-8<cr>
" redraw screen link :redraw
nnoremap <silent> <leader>xl  :nohlsearch<cr>:diffupdate<cr>:syntax sync fromstart<cr><c-l>
" jump to the kwd under cursor
nnoremap <silent> <leader>xf  [I:let nr = input("Which one: ")<Bar>exe "normal " . nr ."[\t"<cr>

" horizontal scroll
nmap zl zL
nmap zh zH
vmap zl zL
vmap zh zH

" command(ex) mode, up/down arrows
cnoremap <c-n> <down>
cnoremap <c-p> <up>

" Shifting sidewards
xnoremap < <gv
xnoremap > >gv

" quickly change font size in GUI
command! Bigger  :let &guifont = substitute(&guifont, '\d\+$', '\=submatch(0)+1', '')
command! Smaller :let &guifont = substitute(&guifont, '\d\+$', '\=submatch(0)-1', '')

" change cursor style dependent on mode
if !has('win32unix')
if empty($TMUX)
    let &t_SI = "\<Esc>]50;CursorShape=1\x7"
    let &t_EI = "\<Esc>]50;CursorShape=0\x7"
    let &t_SR = "\<Esc>]50;CursorShape=2\x7"
else
    let &t_SI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=1\x7\<Esc>\\"
    let &t_EI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=0\x7\<Esc>\\"
    let &t_SR = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=2\x7\<Esc>\\"
endif
endif

"""""""""""""""""""""""""""""" c@@- [ autocmd ]""""""""""""""""""""""""""""""
" .@@. BufWinEnter
" highlight text exceeds 80 columns
au BufWinEnter * let w:m2=matchadd('ErrorMsg', '\%>80v.\+', -1)

" .@@. FileType
au FileType vim,tex   let b:autoformat_autoindent=0
au FileType vim       nmap          ;r       :source<space>$MYVIMRC<cr>
au FileType help      nmap <silent> ;v       :let &syntax=(&syntax == 'help' ? 'text' : 'help')<cr>
au FileType markdown  nmap          ;f<tab>  :TableFormat<cr>
au FileType markdown  nmap          ;t       :Toc<cr>
au FileType markdown  xmap          ;za      :HeaderDecrease<cr>
au FileType markdown  xmap          ;zd      :HeaderIncrease<cr>

" .@@. Editing
" Smarter cursorline
au InsertLeave,WinEnter * set cursorline
au InsertEnter,WinLeave * set nocursorline

" .@@. BufWritePost
au BufWritePost * GitGutter
au BufWritePost $MYVIMRC source $MYVIMRC
if !has('osx') && !has('win32') 
    au BufWritePost ~/.Xdefaults call system('xrdb ~/.Xdefaults')
endif

"""""""""""""""""""""""""""" c@@- [ Vim Options ]""""""""""""""""""""""""""""
set nocompatible               " Do not want vi

filetype plugin indent on      " Load plugins according to detected filetype
syntax on                      " Enable syntax highlighting

set autoindent                 " Indent according to previous line
set expandtab                  " Use spaces instead of tabs
set softtabstop=4              " Tab key indents by 4 spaces
set shiftwidth=4               " >> indents by 4 spaces
set shiftround                 " >> indents to next multiple of 'shiftwidth'
set nowrap

set backspace=indent,eol,start " Make backspace work as you would expect
set hidden                     " Switch between buffers without having to save first
set laststatus=2               " Always show statusline
set display=lastline           " Show as much as possible of the last line

set showmode                   " Show current mode in command-line
set showcmd                    " Show already typed keys when more are expected

set incsearch                  " Highlight while searching with / or ?
set hlsearch                   " Keep matches highlighted.
set ignorecase
set smartcase

" Faster keyword completion
set complete-=i                " disable scanning included files
set complete-=t                " disable searching tags

set ttyfast                    " Faster redrawing.
set lazyredraw                 " Only redraw when necessary.

set splitbelow                 " Open new windows below the current window.
set splitright                 " Open new windows right of the current window.

set cursorline                 " Find the current line quickly.
set wrapscan                   " Searches wrap around end-of-file.
set report=0                   " Always report changed lines.
set synmaxcol=200              " Only highlight the first 200 columns.
set encoding=utf-8

" .@@. sessions & views
set viewoptions=cursor,folds,slash,unix
set sessionoptions=blank,buffers,curdir,folds,globals,help,localoptions,options,resize,tabpages,winpos,winsize

" .@@. list
if has('multi_byte') && &encoding ==# 'utf-8'
    let &listchars = 'tab:▸ ,extends:❯,precedes:❮,nbsp:±'
else
    let &listchars = 'tab:> ,extends:>,precedes:<,nbsp:.'
endif

set list

" The fish shell is not very compatible to other shells and unexpectedly
" breaks things that use 'shell'
if &shell =~# 'fish$'
    set shell=/bin/bash
endif

" Put all temporary files under the same directory.
" https://github.com/mhinz/vim-galore#handling-backup-swap-undo-and-viminfo-files
set backup
set backupdir=$HOME/.vim/files/backup/
set backupext=-vimbackup
set backupskip=
set directory=$HOME/.vim/files/swap//
set updatecount=100
set undofile
set undodir=$HOME/.vim/files/undo/
set viminfo='100,n$HOME/.vim/files/info/viminfo
set viewdir=$HOME/.vim/files/view/

set termguicolors
colorscheme base16-brewer
set background=dark

" .@@. guioptions
if has('win32unix')
elseif has('win32')
    set guifont=DejaVu_Sans_Mono:h9
elseif has('osx')
    set guifont=DejaVu_Sans_Mono:h10
else
    set guifont=DejaVu\ Sans\ Mono\ 9
endif

set guioptions=


" .@@v vim: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab :
